class EndarUserTime < ActiveRecord::Base
	belongs_to :endar_user
	
	validates_numericality_of :user_id
	validates_presence_of :date_begin
	validates_presence_of :date_end
	
	def self.save(upload)
	    name =  upload['datafile'].original_filename
		just_filename = File.basename(name) 
		
		if File.extname(just_filename)!= ".ics"
			redirect_to :controller => 'endar_user_time', :action => 'uploadFileExtensionError'
		end
		  # replace all none alphanumeric, underscore or perioids
		  # with underscore
		just_filename.sub(/[^\w\.\-]/,'_') 
		
	    directory = "public/userics"
	    # create the file path
	    path = File.join(directory, just_filename)
	    # write the file
	    File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
		
		return path
	end
end
